package com.example.finger.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.finger.FingerHelper;
import com.example.finger.R;
import com.example.finger.adpter.LogAdapter;
import com.example.finger.bean.LockLog;
import com.lyxmaster.lib.view.BaseActivity;
import com.lyxmaster.lib.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LYX on 2018/11/23.
 */

public class LogFragment extends MyBaseFragment {
    private ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_log, container, false);

        listView = rootView.findViewById(R.id.listview);
        upDate();

        return  rootView;
    }

    public void upDate(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final List<LockLog> list = FingerHelper.getInStance().dbManager.selector(LockLog.class).orderBy("time",true).findAll();
                    ((Activity)(getContext())).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(list != null){
                                listView.setAdapter(new LogAdapter(list, getContext()));
                            }else {
                                listView.setAdapter(new LogAdapter(new ArrayList<LockLog>(), getContext()));
                            }
                        }
                    });
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
